package com.heima.Serivce.impl;

import com.heima.model.ES.pojos.UserSearchDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.pojos.ApAssociateWords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApAssociateWordsService implements com.heima.Serivce.ApAssociateWordsService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public ResponseResult GetAssociateWords(UserSearchDto userSearchDto) {
        Query query=Query.query(Criteria.where("associateWords").regex(".*?\\" + userSearchDto.getSearchWords() + ".*"));
        if(userSearchDto.getPageSize()>10)
            userSearchDto.setPageSize(10);
        query.limit(userSearchDto.getPageSize());
        List<ApAssociateWords> associateWords = mongoTemplate.find(query, ApAssociateWords.class);
        return ResponseResult.okResult(associateWords);
    }
}
